﻿@inject AuthenticationStateProvider authenticationStateProvider
@inject NavigationManager navigationManager

@if (showNotAuthorizedMessage)
{
    <section class="section" id="not-authorized-message">
        <div class="container">
            <div class="notification is-danger">
            </div>
        </div>
    </section>
}

@code {

    private bool showNotAuthorizedMessage = false;

    protected override async Task OnInitializedAsync()
    {
        var state = await authenticationStateProvider.GetAuthenticationStateAsync();

        if (!state.User.Identity.IsAuthenticated)
        {
            // If the user is not authenticated redirect them to the sign in page
            navigationManager.NavigateTo("/account/signin?returnUrl=" + System.Net.WebUtility.UrlEncode(new Uri(navigationManager.Uri).PathAndQuery));
        }
        else
        {
            // If the user is signed in, but authorization failed, display a message
            showNotAuthorizedMessage = true;
        }
    }
}